﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-06-09 14:13:18
 * 功能：开屏广告
**************************************************************************************/

using Game.Framework;

public class Open : AdBase
{
    public Open(string adUnitId) : base(adUnitId)
    {
        m_AdType = EAdType.Splash;
    }

    public override void Initialize()
    {
        MaxSdkCallbacks.AppOpen.OnAdLoadedEvent += OnLoadedEvent;
        MaxSdkCallbacks.AppOpen.OnAdLoadFailedEvent += OnLoadFailedEvent;
        MaxSdkCallbacks.AppOpen.OnAdDisplayedEvent += OnDisplayedEvent;
        MaxSdkCallbacks.AppOpen.OnAdDisplayFailedEvent += OnAdFailedToDisplayEvent;
        MaxSdkCallbacks.AppOpen.OnAdClickedEvent += OnClickedEvent;
        MaxSdkCallbacks.AppOpen.OnAdHiddenEvent += OnHiddenEvent;
        MaxSdkCallbacks.AppOpen.OnAdRevenuePaidEvent += OnAdRevenuePaidEvent;

        LoadAd();
    }

    protected override void LoadAd()
    {
        base.LoadAd();
        MaxSdk.LoadAppOpenAd(m_AdUnitId);
        LogManager.Log($"加载Max广告，type:{m_AdType}", "广告");
    }

    protected override void OnAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo info)
    {
        m_TotalReward = GameDataMgr.Instance.OpenAdTotalRevenue;
        base.OnAdRevenuePaidEvent(adUnitId, info);
        GameDataMgr.Instance.OpenAdTotalRevenue = m_TotalReward;
    }

    /// <summary>显示开屏广告</summary>
    public override void Show(ETriggerAdType type)
    {
        m_TriggerType = type;
        if (IsReady())
        {
            EventCenter.Instance.Trigger(GameEventName.ON_BE_ABOUT_TO_SHOW_AD, m_AdType, m_TriggerType);
            MaxSdk.ShowAppOpenAd(m_AdUnitId);
            LogManager.Log($"开始Max开屏广告播放,adUnitID:{m_AdUnitId}", "广告");
        }
        else
        {
            EventCenter.Instance.Trigger(GameEventName.ON_SHOW_FAILED_AD, m_AdType, m_TriggerType);
            LogManager.Log($"Max开屏广告未准备好，无法播放！！", "广告");
        }
    }

    public bool IsReady() => MaxSdk.IsAppOpenAdReady(m_AdUnitId);
}